/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yinshi.javafile;

import java.util.Objects;

/**
 *
 * @author yinshi
 */
public class Identifier {

    private final String text;

    public Identifier(String text) {
        if (!isJavaIdentifer(text)) {
            throw new IllegalArgumentException();
        }
        this.text = text;
    }

    public int length() {
        return text.length();
    }

    public static boolean isJavaIdentifer(String text) {
        Objects.requireNonNull(text);

        int length = text.length();
        if (length == 0) {
            return false;
        }

        char ch = text.charAt(0);
        if (!Character.isJavaIdentifierStart(ch)) {
            return false;
        }

        for (int i = 1; i < length; i++) {
            if (!Character.isJavaIdentifierPart(text.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
